package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.query.BidRequestAuditQueryObject;
import cn.wolfcode.p2p.business.query.CreditTransferQueryObject;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.ICreditTransferService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/**
 * 前台-我要投资
 */
@Controller
public class InvestController {

    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private ICreditTransferService creditTransferService;

    /**
     * 准备投资的主页面,此方法不查询数据
     *
     * @return
     */
    @RequestMapping("invest")
    public String invest() {

        return "invest";
    }

    /**
     * 查询数据共享
     *
     * @return
     */
    @RequestMapping("invest_list")
    public String invest_list(@ModelAttribute("qo") BidRequestAuditQueryObject qo, Model model) {

        if (qo.getBidRequestType().intValue() == Constants.BIDREQUEST_TYPE_NORMAL) {
            //只查询招标中,已完成的
            if (qo.getState() == -1) {
                qo.setBidRequestState(new int[]{Constants.BIDREQUEST_STATE_BIDDING,
                        Constants.BIDREQUEST_STATE_COMPLETE_PAY_BACK});
            }
            model.addAttribute("pageInfo", bidRequestService.listForPage(qo));

            return "invest_list";
        } else {
            //债权标投资列表
            CreditTransferQueryObject cqo = new CreditTransferQueryObject();
            //状态:招标中
            cqo.setBidRequestState(Constants.TRANSFER_STATE_BIDDING);
            cqo.setCurrentPage(qo.getCurrentPage());
            model.addAttribute("pageInfo", creditTransferService.queryForInvestPage(cqo));
            return "credittransfer_list";

        }
    }

    @RequestMapping("borrow_info")
    public String borrowInfo(Long id, Model model) {
        //获取借款对象
        BidRequest bidRequest = bidRequestService.getById(id);
        model.addAttribute("bidRequest", bidRequest);

        //借款人
        LoginInfo borrowUser = bidRequest.getCreateUser();


        if (borrowUser.getUserType() != LoginInfo.USERTYPE_MGRSITE) {
            //实名信息

            //借款人的userInfo
            UserInfo userInfo = userInfoService.getById(borrowUser.getId());
            model.addAttribute("userInfo", userInfo);

            RealAuth realAuth = realAuthService.getById(userInfo.getRealAuthId());
            model.addAttribute("realAuth", realAuth);
        }

        //登录用户
        LoginInfo loginUser = UserContext.getLoginInfo();
        if (loginUser != null) {
            if (borrowUser.getId().equals(loginUser.getId())) {
                //判断如果当前用户是借款人
                model.addAttribute("self", true);

            } else {
                //判断如果当前用户是投资人
                Account investUser = accountService.getById(loginUser.getId());
                model.addAttribute("account", investUser);

            }
        }

        return "borrow_info";
    }

    /**
     * 马上投标-
     *
     * @param bidRequestId
     * @param amount
     */
    @RequestMapping("borrow_bid")
    @ResponseBody
    public JsonResult borrowBid(Long bidRequestId, BigDecimal amount) {
        bidRequestService.bid(bidRequestId, amount);
        return JsonResult.instance();
    }

}
